import axios from "axios";

const request = axios.create({
  baseURL: "/api",
  timeout: 10000,
});

// 请求拦截器
request.interceptors.request.use(
  (config) => {
    // 添加请求头
    config.headers["Content-Type"] = "application/json;charset=utf-8";
    const token = localStorage.getItem("token");
    if (token) {
      config.headers["token"] = token;
    }
    return config;
  },
  (error) => {
    return Promise.reject(error);
  }
);

// 响应拦截器
request.interceptors.response.use(
  (response) => {
    // 响应数据
    const res = response.data;
    return res;
  },
  (error) => {
    return Promise.reject(error);
  }
);

export default request;
